package se.webbzon.boltzmann.bootstrap;

import java.io.File;
import java.io.IOException;
import java.security.ProtectionDomain;

import se.webbzon.oschi01.bootstrap.Application;
import se.webbzon.oschi01.bootstrap.BootstrapClassLoader;
import se.webbzon.oschi01.bootstrap.BootstrapException;
import se.webbzon.oschi01.bootstrap.BootstrapperUtils;
import se.webbzon.oschi01.bootstrap.OS;
import se.webbzon.oschi01.bootstrap.OSFamily;
import se.webbzon.oschi01.bootstrap.j3d.J3DBootstrap;

public class BoltzmannBootstrap extends J3DBootstrap {

	/*============================================================
	Protected Methods
	============================================================*/
	
	@Override protected ClassLoader getClassLoader(ClassLoader classLoader, ProtectionDomain protectionDomain, Application application) throws BootstrapException {
		ClassLoader cl = super.getClassLoader(classLoader, protectionDomain, application);
		if (cl instanceof BootstrapClassLoader) try {
			BootstrapClassLoader bcl = (BootstrapClassLoader) cl;
			String dataModel = System.getProperty("sun.arch.data.model");
			OS os = BootstrapperUtils.getOS();
			OSFamily family = os.getFamily();
			
			switch (family) {
			case WINDOWS:
				if (dataModel.equals("64"))
					bootstrapWindows64(bcl);
				else
					bootstrapWindows(bcl);
				break;
			case LINUX:
				if (dataModel.equals("64"))
					bootstrapLinux64(bcl);
				else
					bootstrapLinux(bcl);
				break;
			case MAC:
				bootstrapMac(bcl);
				break;
			default:
				throw new BootstrapException("Platform not supported!");
			}
			
		} catch (IOException e) {
			throw new BootstrapException(e);
		}
		
		return cl;
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Bootstraps the given class loader for Windows. **/
	private final void bootstrapWindows(BootstrapClassLoader classLoader) throws IOException {
		String tmpDir = System.getProperty("java.io.tmpdir");
		final File dx8 = new File(tmpDir + "\\jinput-dx8.dll");
		final File raw = new File(tmpDir + "\\jinput-raw.dll");
		BootstrapperUtils.copyFromJar("jinput/archive/dist/jinput-dx8.dll",dx8);
		BootstrapperUtils.copyFromJar("jinput/archive/dist/jinput-raw.dll",raw);
		classLoader.addLibrary("jinput-dx8", dx8.getAbsolutePath());
		classLoader.addLibrary("jinput-raw", raw.getAbsolutePath());
	}
	
	/** Bootstraps the given class loader for Windows x64. **/
	private final void bootstrapWindows64(BootstrapClassLoader classLoader) throws IOException {
		String tmpDir = System.getProperty("java.io.tmpdir");
		final File dx8 = new File(tmpDir + "\\jinput-dx8_64.dll");
		final File raw = new File(tmpDir + "\\jinput-raw_64.dll");
		BootstrapperUtils.copyFromJar("jinput/archive/dist/jinput-dx8_64.dll",dx8);
		BootstrapperUtils.copyFromJar("jinput/archive/dist/jinput-raw_64.dll",raw);
		classLoader.addLibrary("jinput-dx8_64", dx8.getAbsolutePath());
		classLoader.addLibrary("jinput-raw_64", raw.getAbsolutePath());
	}
	
	/** Bootstraps the given class loader for Linux. **/
	private final void bootstrapLinux(BootstrapClassLoader classLoader) throws IOException {
		String tmpDir = System.getProperty("java.io.tmpdir");
		final File jinput = new File(tmpDir + "/libjinput-linux.so");
		BootstrapperUtils.copyFromJar("jinput/archive/dist/libjinput-linux.so",jinput);
		classLoader.addLibrary("jinput-linux", jinput.getAbsolutePath());
	}
	
	/** Bootstraps the given class loader for Linux x64. **/
	private final void bootstrapLinux64(BootstrapClassLoader classLoader) throws IOException {
		String tmpDir = System.getProperty("java.io.tmpdir");
		final File jinput = new File(tmpDir + "/libjinput-linux64.so");
		BootstrapperUtils.copyFromJar("jinput/archive/dist/libjinput-linux64.so",jinput);
		classLoader.addLibrary("jinput-linux64", jinput.getAbsolutePath());
	}

	/** Bootstraps the given class loader for Max OS X. **/
	private final void bootstrapMac(BootstrapClassLoader classLoader) throws IOException {
		String tmpDir = System.getProperty("java.io.tmpdir");
		final File jinput = new File(tmpDir + "/libjinput-osx.jnilib");
		BootstrapperUtils.copyFromJar("jinput/archive/dist/libjinput-osx.jnilib",jinput);
		classLoader.addLibrary("jinput-osx", jinput.getAbsolutePath());
	}

}
